package simbot.example.demo.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class JsonShortDateSerializer extends JsonSerializer<Long> {

    @Override
    public void serialize(Long time, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
        if (time == 0) {
            jsonGenerator.writeString("");
        } else {
            String formattedDate = "";
            Date date = new Date(time);

            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

            formattedDate = dateFormat.format(date);

            jsonGenerator.writeString(formattedDate);
        }

    }
}
